package org.extframework.ld.http.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extframework.ld.bean.model.IUser;
import org.extframework.ld.conf.ParameterNames;
import org.extframework.ld.http.global.ExtSystemGlobal;

/**
 * @author : Dong.Li
 * @created : 2013-1-22
 * @version : 1.0
 * @description :
 */
public class ExtHttpSessionAttributeListener implements
		HttpSessionAttributeListener {

	private final transient Log logger = LogFactory.getLog(super.getClass());

	public void attributeAdded(HttpSessionBindingEvent bindingEvent) {
		if (bindingEvent.getName().equals(ParameterNames.LOGIN_USER_MARK)) {
			try {
				ExtSystemGlobal.setUserSession((IUser) bindingEvent.getValue());
			} catch (Exception e) {
				logger.error(" attributeAdded [methord]", e);
			}
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent bindingEvent) {
		if (bindingEvent.getName().equals(ParameterNames.LOGIN_USER_MARK)) {
			try {
				ExtSystemGlobal.removeUserSession();
			} catch (Exception e) {
				logger.error(" attributeRemoved [methord]", e);
			}
		}
	}

	public void attributeReplaced(HttpSessionBindingEvent bindingEvent) {
		if (bindingEvent.getName().equals(ParameterNames.LOGIN_USER_MARK)) {
			try {
				ExtSystemGlobal.setUserSession((IUser) bindingEvent.getValue());
			} catch (Exception e) {
				logger.error(" attributeReplaced [methord]", e);
			}
		}
	}

}
